package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Users;
import com.ruoyi.common.core.domain.model.LoginAppBody;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.conbined_product.domain.CombineProductData;
import com.ruoyi.conbined_product.service.ICombinedProductDataService;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.service.AppLoginService;
import com.ruoyi.products.domain.CombinedProductData;
import com.ruoyi.products.domain.Products;
import com.ruoyi.products.service.IProductsService;
import com.ruoyi.productspecifications.domain.Productspecifications;
import com.ruoyi.productspecifications.service.IProductspecificationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api("APP登录管理")
@RestController
@RequestMapping("/app")
public class AppLoginController extends BaseController {
    @Resource
    private AppLoginService appLoginService;
    @Resource
    private IProductsService productsService;
    @Autowired
    private ICombinedProductDataService combinedProductDataService;
    @Resource
    private IProductspecificationsService productspecificationsService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginAppBody loginAppBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appLoginService.login(loginAppBody.getUsername(), loginAppBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("/userInfo")
    public AjaxResult userInfo() {
        Users appUser = getLoginAppUser().getAppUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("appUser", appUser);
        return ajax;
    }
    /**
     * 根据商品类型查询商品列表
     */
    @GetMapping("/type/{productType}")
    public AjaxResult listByType(@PathVariable("productType") String productType) {
        List<CombinedProductData> productsList = productsService.selectProductsByProductType(productType);
        return AjaxResult.success(productsList);
    }
    /**
     * 根据商品Id查询商品详细信息
     */
    @GetMapping(value = "/{productId}")
    public AjaxResult getCombinedProductData(@PathVariable("productId") Long productId)
    {
        if (StringUtils.isNull(productId))
        {
            return AjaxResult.error("商品ID不能为空");
        }
        CombinedProductData products = productsService.selectCombineProductDataByProductId(productId);
        if (products == null)
        {
            return AjaxResult.error("商品不存在");
        }
        return AjaxResult.success(products);
    }
    /**
     * 根据商品Id查询商品的规格尺寸
     */
    @GetMapping("/Info/{productId}")
    public AjaxResult getInfoProduct(@PathVariable("productId") Long productId){
        if (StringUtils.isNull(productId))
        {
            return AjaxResult.error("商品ID不能为空");
        }
        List<Productspecifications> products = productspecificationsService.selectProductspecificationsByProductId(productId);
        if (products == null)
        {
            return AjaxResult.error("商品不存在");
        }

        return AjaxResult.success(products);
    }
    /**
     * 查询商品重置列表
     */
    @GetMapping("/product/list")
    public AjaxResult list(CombineProductData combineProductData)
    {
        List<CombineProductData> list = combinedProductDataService.selectCombinedProductDataList(combineProductData);
        System.out.println("查询商品");
        return AjaxResult.success(getDataTable(list));
    }
}
